package yyy.ab.cfgBean;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import yyy.ab.common.cfgBean.FileProperties;
import yyy.ab.common.converter.*;

/**
 * WebMvcConfigurer
 */
@Configuration
@EnableWebMvc
public class AbConfigurerAdapter implements WebMvcConfigurer {

    /**
     * 文件配置
     */
    private final FileProperties properties;

    public AbConfigurerAdapter(FileProperties properties) {
        this.properties = properties;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }

    /**
     * 自定义视图V、控制器C 映射
     *
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("", "/dashboard");
        registry.addViewController("/login").setViewName("login");
        registry.addViewController("/dashboard").setViewName("dashboard/index");
        registry.addViewController("/welcome").setViewName("dashboard/welcome");
        //错误页面
        registry.addViewController("/400").setViewName("error/400");
        registry.addViewController("/403").setViewName("error/403");
        registry.addViewController("/404").setViewName("error/404");
        registry.addViewController("/500").setViewName("error/500");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /** 本地文件上传路径 */
        FileProperties.ElPath path = properties.getPath();
        String avatarUtl = "file:" + path.getAvatar().replace("\\", "/");
        String pathUtl = "file:" + path.getPath().replace("\\", "/");
        registry.addResourceHandler("/avatar/**").addResourceLocations(avatarUtl).setCachePeriod(0);
        registry.addResourceHandler("/file/**").addResourceLocations(pathUtl).setCachePeriod(0);
        /* 第三方静态资源：swagger配置、druid等 */
        registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/");//.setCachePeriod(0);
        registry.addResourceHandler("/ab-ui/**").addResourceLocations("classpath:/static/ab-ui/").setCachePeriod(0);
        registry.addResourceHandler("/cockpit-ui/**").addResourceLocations("classpath:/static/cockpit-ui/").setCachePeriod(0);
        registry.addResourceHandler("/admin-ui/**").addResourceLocations("classpath:/static/admin-ui/").setCachePeriod(0);
    }

//    /**
//     * 解决主键Long类型返回给页面时，页面精度丢失的问题,时间格式化返回
//     * @param converters
//     */
//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//        //格式化json数据格式
//        FastJsonConfig fastJsonConfig = new FastJsonConfig();
//        //序列化时避免精度丢失，转换为字符串
//        SerializeConfig serializeConfig = SerializeConfig.globalInstance;
//        serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
//        serializeConfig.put(Long.class, ToStringSerializer.instance);
//        serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
//        fastJsonConfig.setSerializeConfig(serializeConfig);
////        fastJsonConfig.setDateFormat("yyyy-HH-dd HH:mm:ss"); //响应时间格式化
//        fastConverter.setFastJsonConfig(fastJsonConfig);
//        //处理中文乱码问题
//        List<MediaType> fastMediaTypes = new ArrayList<>();
//        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
//        fastMediaTypes.add(MediaType.APPLICATION_JSON);
//        fastConverter.setSupportedMediaTypes(fastMediaTypes);
//        converters.add(0,fastConverter);
//    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new DateConverter());
        registry.addConverter(new StringConverter());
        registry.addConverter(new LongConverter());
        registry.addConverter(new IntegerConverter());
        registry.addConverterFactory(new StringToEnumConverterFactory());
    }
}
